home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Interapplication Communication / AECDEV⁄AEDAEMON / AEDaemonMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-09  |  5.0 KB  |  127 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #   Apple Developer Technical Support
  4. #
  5. #   AEDeamon
  6. #   A small faceless background-only application for sending AppleEvents
  7. #
  8. #   main.c  -   C Source
  9. #
  10. #   Copyright © 1991 Apple Computer, Inc.
  11. #   All rights reserved.
  12. #
  13. #   Versions:   
  14. #               1.0               08/91       C.K. Haun <TR>
  15. #   
  16. #   Components:
  17. #                           AEDeamonMain.c
  18. #                           AEDeamonAEvents.c
  19. #                            AEDeamonPPCStuff.c
  20. #
  21. #    This is a faceless background process who's whole mission in
  22. #    life is to send AppleEvents for code that can't (like the accompaning
  23. #    AECDEV).  It will send any AppleEvent it gets through it's PPC port.
  24. #   
  25. #   The main things to note are....
  26. #   1)  Look at the SIZE resource for the flag settings you'll
  27. #       need to let the Finder™ know that you only want to work
  28. #       in the background.
  29. #   2)  Notice that you really do have your own heap and your own
  30. #       event loop.  You can do anything a foreground application
  31. #       can do, send AppleEvents, PPC stuff, anything else you'd
  32. #       like EXCEPT a graphic interface.
  33. #   3)  NOTE that no managers are started up.  You cannot start up
  34. #        Window, Menu, Dialogs, or anything else that 
  35. #       deals with the graphic interface.  Just leave them out.
  36. #        You CAN start up QuickDraw if you want to do some graphic 
  37. #        processing, but DO NOT draw to the screen, only use things 
  38. #        like offscreen grafports or GWorlds.  Or you may 
  39. #        want to start it to use Random().....
  40. #
  41. #   Of course, a backgrounder can be launched from the Finder™ 
  42. #   with a double-click.  However, if you'd like the backgrounder
  43. #   to perform some useful service for your main application, driver,
  44. #   DA, or whatever, you will want it running all the time.
  45. #   The BEST way to insure this is to install the backgrounder in the
  46. #   StartUp Items folder in the system folder.  This will insure that
  47. #   it is always launched, and it will also reduce memory fragmentation
  48. #   since it will be installed at startup time.  You can search for it
  49. #   when you need it with IPCListPorts (see the PPC toolbox documentation
  50. #    or the AECDEV code).
  51. #   Optionally, you can use the LaunchApplication trap to launch it 
  52. #   when you need it, and kill it when you're done.  But this could 
  53. #   cause some multiFinder heap fragmentation.
  54. #
  55. #   And remember, it's a backgrounder, you can't see it.  To kill it
  56. #   use TaskIt or ProcDoggie.
  57. #
  58. #   ---------------------------------------------------------------
  59. #   Use this sample as a starting point, and adapt its' routines to 
  60. #   meet the specific needs of your project.  
  61. #   This sample will grow as more edition types are implemented,
  62. #   periodically check AppleLink for a later, expanded sample.
  63. #   This application is an example of the form of a Macintosh 
  64. #   application; it is NOT a template. It is NOT intended to be 
  65. #   used as a foundation for the next world-class, best-selling, 
  66. #   600K application. A stick figure drawing of the human body may 
  67. #   be a good example of the form for a painting, but that does not 
  68. #   mean it should be used as the basis for the next Mona Lisa.
  69. #
  70. #
  71. ------------------------------------------------------------------------------*/
  72.  
  73.  
  74. #include "AEDaemon.h"
  75.  
  76. extern MyPPCRecPtrDeamon ourPPCPtr;
  77. extern Ptr readBuffer;
  78. extern Handle dataHandle;
  79. Boolean gQuit = false;
  80. EventRecord ERecord;
  81. Boolean gHasAppleEvents;
  82. Boolean gReadAgain;
  83. Boolean gReadPending;
  84. unsigned long gMySleep = 200;  /* Long time.  Change this if  */
  85. /* you'd like to do null processing. Just keep in mind that the */
  86. /* user does NOT know that you exisist, and if you are eating */
  87. /* up a bunch of time the user will not know why his or her */
  88. /* machine is slowing down. */
  89.  
  90. main()
  91. {
  92.     long ticksStart;
  93.     /* We are NOT initializing any managers.  We're in the background, with no */
  94.     /* face, we can't use windows or dialogs or menus.  If you need to talk to the */
  95.     /* user you can post a notification, or launch an application to comunicate */
  96.     /* Passing an AppleEvent in the launchapplication trap could do the */
  97.     /* communication for you. */
  98.     
  99.     /* no nothing but events */
  100.     
  101.     InitAEStuff();
  102.     /* now initialize our PPC connection so people know we're around */
  103.     if(PPCInit() != noErr)ExitToShell(); /* bail */
  104.     ourPPCPtr = (MyPPCRecPtrDeamon)NewPtrClear(sizeof(MyPPCRecDeamon));
  105.     readBuffer = NewPtr(kOneK);
  106.     dataHandle = NewHandle(nil);                            /* get an empty handle to start */
  107.     /* check memory, bail if bad */
  108.     if(ourPPCPtr == nil || dataHandle == nil || readBuffer== nil)ExitToShell();
  109.     InformTheWorld();
  110.     ticksStart = TickCount();
  111.     /* no nothing but high level events */
  112.     while (gQuit == false) {
  113.         WaitNextEvent(highLevelEventMask, &ERecord, gMySleep, 0);
  114.         if (ERecord.what == kHighLevelEvent)
  115.             DoHighLevel(&ERecord);
  116.         if (gReadPending)
  117.             CollectLastData();
  118.         
  119.     }
  120.     CloseOffTheWorld();
  121. }
  122.  
  123. #undef __BUILDINGDEAMON__
  124. #undef __AEDMAIN__
  125.  
  126.  
  127.